<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}
require_once('adminApp.php');
/** 系统管理员角色控制类 */
class RoleController extends AdminAppController {	
	public function __construct() {		
        parent::__construct();
	}
 	/** 角色列表 */
 	public function actionDefault(){ 		
 		$page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
 		$size = 10; 		
 		$cond = "1=1 ";
 		if($_REQUEST['title']){
 			$cond .= " and ROLE_NAME like '%".$_REQUEST['title']."%'";
 		}
 		if($_REQUEST['state']){
 			$cond .= " and STATE=".$_REQUEST['state'];
 		} 		
 		$res = array();
 		$accountLogic = $this->Logic('AccountLogic');
 		try{
 			$res = $accountLogic->getRoleList("*", $cond, "order by id desc", $page, $size);
			$par = array ();
			$res['page'] = page :: listPage($par, $res["count"], $page, $size);
 		}catch(Exception $e){
 			echo $e->getMessage();
 		}		
		T::$tpl->set("res", $res);
 	}
 	/** 添加角色 */
 	public function actionAdd(){
 		T::$tplFile = "admin/role/act"; 		
 		if($_POST['actname']=='save'){
 			$this->SaveData($_POST, 'add');
 		} 		
 	}
 	/** 修改角色 */
 	public function actionEdit(){
 		T::$tplFile = "admin/role/act"; 		
 		$id = $_REQUEST['id'];
 		if($id){ 			
 			if($_POST['actname']=='save'){
	 			$this->SaveData($_POST, 'edit');
	 		}else{
	 			//显示
	 			$roleItem = $this->Logic('AccountLogic')->getRoleItem("*","ID=".$id);
	 			T::$tpl->set('roleItem', $roleItem);
	 		}
 		}else{
 			$this->sysMessage("修改失败,要修改的ID异常");
 		}		
 	} 	
 	/** 保存添加、修改 */
 	private function SaveData($data, $type){
 		if($type=='add'){
 			$res = $this->Logic('AccountLogic')->addRoleItem($data);
 			if($res){
 				$this->sysMessage("添加成功");
 			}else{
 				$this->sysMessage("添加失败,请重试");
 			}
 		}elseif($type=='edit'){
 			$res = $this->Logic('AccountLogic')->updateRoleItem($data, "ID=".$data['id']);
 			if($res){
 				$this->sysMessage("修改成功");
 			}else{
 				$this->sysMessage("修改失败,请重试");
 			}
 		}else{
 			$this->sysMessage("未知操作..");
 		}
 	} 	
 	/** 删除角色 */
 	public function actionDel(){
 		T::$tplFile = "admin/role/act";
 	}	
 	
}//End CLass
?>